package com.coder.bike.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coder.bike.entity.BikeMaker;
import com.coder.bike.service.IBikeMakerService;
import com.coder.bike.utils.Result;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 沈诩宸
 * @since 2025-04-22
 */
@RestController
@RequestMapping("/bike/bikeMaker")
public class BikeMakerController {
    @Resource
    private  IBikeMakerService bikeMakerService;
    @PostMapping("/{start}/{size}")
    public Result search(@PathVariable int start,
                         @PathVariable int size,
                         @RequestBody BikeMaker autoMaker) {
        Page<BikeMaker> page = bikeMakerService.search(start, size,
                autoMaker);
        return Result.success().setData(page);
    }
    @PostMapping
    public Result save(@RequestBody BikeMaker autoMaker) {
        return bikeMakerService.save(autoMaker) ? Result.success() :
                Result.fail();
    }
    @PutMapping
    public Result update(@RequestBody BikeMaker autoMaker) {
        return bikeMakerService.updateById(autoMaker) ? Result.success() :
                Result.fail();
    }
    @DeleteMapping("/{ids}") //1,2,3
    public Result delete(@PathVariable String ids) {
        List<Integer> list =
                Arrays.stream(ids.split(",")).map(Integer::parseInt).toList();
        return bikeMakerService.removeByIds(list) ? Result.success() :
                Result.fail();
    }
    @GetMapping("selectAll")
    public Result selectAll() {
        List<BikeMaker> list =  bikeMakerService.list();
        return Result.success().setData(list);
    }
}
